package com.one.blocks.mvc.convert;

import com.one.blocks.mvc.domain.BaseDomain;
import com.one.blocks.mvc.domain.Id;
import org.mapstruct.InheritConfiguration;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.Named;

import java.io.Serializable;


/**
 * @author linfuxin Created on 2022-02-14 11:21:00
 */
public interface BaseDomainConvert<T extends BaseDomain<? extends Serializable> & Id<? extends Serializable>, V, Q> extends BaseConvert<T, V, Q> {

    @Named("voToEntityIgnoreId")
    @Mapping(target = Id.FIELD_NAME_ID, ignore = true)
    @Mapping(target = BaseDomain.FIELD_NAME_CREATE_ID, expression = "java(SubjectHelper.getSubjectId())")
    @Mapping(target = BaseDomain.FIELD_NAME_CREATE_TIME, expression = "java(SubjectHelper.getAccessTime())")
    @Mapping(target = BaseDomain.FIELD_NAME_UPDATE_ID, ignore = true)
    @Mapping(target = BaseDomain.FIELD_NAME_UPDATE_TIME, ignore = true)
    T voToEntityIgnoreId(V v);

    @Named("voToEntity")
    @Mapping(target = BaseDomain.FIELD_NAME_CREATE_ID, ignore = true)
    @Mapping(target = BaseDomain.FIELD_NAME_CREATE_TIME, ignore = true)
    @Mapping(target = BaseDomain.FIELD_NAME_UPDATE_ID, expression = "java(SubjectHelper.getSubjectId())")
    @Mapping(target = BaseDomain.FIELD_NAME_UPDATE_TIME, expression = "java(SubjectHelper.getAccessTime())")
    T voToEntity(V v);

    @Named("voToEntityOverride")
    @Mapping(target = Id.FIELD_NAME_ID, ignore = true)
    @Mapping(target = BaseDomain.FIELD_NAME_CREATE_ID, ignore = true)
    @Mapping(target = BaseDomain.FIELD_NAME_CREATE_TIME, ignore = true)
    @Mapping(target = BaseDomain.FIELD_NAME_UPDATE_ID, expression = "java(SubjectHelper.getSubjectId())")
    @Mapping(target = BaseDomain.FIELD_NAME_UPDATE_TIME, expression = "java(SubjectHelper.getAccessTime())")
    @InheritConfiguration(name = "voToEntity")
    void voToEntityOverride(V v, @MappingTarget T t);
}